package bit;

import java.io.*;
import java.util.Arrays;

/**
 * @author ZJX
 * @version 1.0
 * @descption
 * @since 2025/11/17 20:12
 */
public class Day26 {
//    public static void main(String[] args) throws IOException {
//        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//        PrintWriter out = new PrintWriter(System.out);
//        in.nextToken();
//        int n = (int) in.nval; // 数组长度
//        int[] nums = new int[n];
//        for (int i = 0; i < n; i++) {
//            in.nextToken();
//            nums[i] = (int) in.nval;
//        }
//        int[] dp = new int[n + 1];
//        dp[0] = 0;
//        dp[1] = nums[0];
//        for (int i = 2; i <= n; i++) {
//            dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
//        }
//        out.println(dp[n]);
//        out.close();
//    }

//    public static void main(String[] args) throws IOException {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        PrintWriter out = new PrintWriter(System.out);
//        String s = br.readLine();
//        out.println(findShortestPalindrome(s));
//        out.close();
//    }

    private static int findShortestPalindrome(String s) {
        int n = s.length();
        for (int i = 1; i < n; i++) {
            if (s.charAt(i - 1) == s.charAt(i)) {
                return 2;
            }
        }
        for (int i = 1; i < n - 1; i++) {
            if (s.charAt(i - 1) == s.charAt(i + 1)) {
                return 3;
            }
        }
        return -1;
    }

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(System.out);
        in.nextToken();
        int n = (int) in.nval; // 数组长度
        in.nextToken();
        int p = Math.abs((int) in.nval); // 范围变量
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            in.nextToken();
            nums[i] = (int) in.nval;
        }
        Arrays.sort(nums);
        int maxCount = 0;
        for (int num : nums) {
            int minRange = num - p;
            int maxRange = num + p;
            int count = findCountInRange(nums, minRange, maxRange);
            if (count > maxCount) {
                maxCount = count;
            }
        }
        out.println(maxCount);
        out.close();
    }


    private static int findCountInRange(int[] nums, int minRange, int maxRange) {
        int leftIndex = findLeftIndex(nums, minRange);
        int rightIndex = findRightIndex(nums, maxRange);
        return rightIndex - leftIndex + 1;
    }

    private static int findLeftIndex(int[] nums, int target) {
        if (target <= nums[0]) {
            return 0;
        }
        int left = 0, right = nums.length - 1;
        while (right >= left) {
            int mid = left + (right - left) / 2;
            if (nums[mid] >= target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }

    private static int findRightIndex(int[] nums, int target) {
        int n = nums.length;
        if (target >= nums[n - 1]) {
            return nums.length - 1;
        }
        int left = 0, right = n - 1;
        while (right >= left) {
            int mid = left + (right - left) / 2;
            if (nums[mid] <= target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return right;
    }


}

